<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="departmentList" row-key="id"
      :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="机构名称" align="center" prop="orgName" />
      <el-table-column label="组织机构全称" align="center" prop="fullName" />
      <el-table-column label="会议调度员" align="center" prop="adminUserName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['gzt:department:edit']">设置会议调度员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改赣政通部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="deptUser" :rules="rules" label-width="80px">
        <el-form-item label="手机号">
          <el-input v-model="deptUser.phone" placeholder="请输入调度员手机号">
            <template slot="append">
              <el-button type="primary" @click="queryPhone">
                查询
                <i class="el-icon-search el-icon--right"></i>
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="调度员">
          <el-select placeholder="请选择调度员"
              clearable v-model="deptUser.adminGztUserId">
            <el-option v-for="user in phoneUser" :key="user.userId" :label="`${user.deptName}/${user.userName}`" :value="user.userId" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDepartment,
    listDepartmentUser,
    getDepartment,
    delDepartment,
    addDepartment,
    updateDepartment
  } from "@/api/gzt/department";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Department",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 赣政通部门表格数据
        departmentList: [],
        // 赣政通部门树选项
        departmentOptions: [],
        // 弹出层标题
        title: "",
        //根据手机号查询出的部门信息
        phoneUser: [],
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          rowState: null,
          lastUpdateTime: null,
          fld: null,
          fldText: null,
          step: null,
          orgName: null,
          fullName: null,
          address: null,
          tel: null,
          zipCode: null,
          mobile: null,
          email: null,
          fax: null,
          website: null,
          pinyin: null,
          pinyinInitial: null,
          isDirectory: null,
          isUnit: null,
          isUnitText: null,
          isTemporary: null,
          isTemporaryText: null,
          status: null,
          statusText: null,
          hytx: null,
          dlqy: null,
          orgLevel: null,
          orgLevelText: null,
          orgFunction: null,
          showOrder: null,
          contactUser: null,
          unitCode: null,
          unitDomain: null,
          cabinetCode: null
        },
        deptUser:{
          phone:null,
          id:null,
          adminGztUserId:null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询赣政通部门列表 */
      getList() {
        this.loading = true;
        listDepartment(this.queryParams).then(response => {
          this.departmentList = this.handleTree(response.data, "id", "fid");
          this.loading = false;
        });
      },
      /** 转换赣政通部门数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.orgName,
          children: node.children
        };
      },
      /** 查询赣政通部门下拉树结构 */
      getTreeselect() {
        listDepartment().then(response => {
          this.departmentOptions = [];
          const data = {
            id: 0,
            orgName: '顶级节点',
            children: []
          };
          data.children = this.handleTree(response.data, "id", "fld");
          this.departmentOptions.push(data);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          rowState: null,
          lastUpdateTime: null,
          fld: null,
          fldText: null,
          step: null,
          orgName: null,
          fullName: null,
          address: null,
          tel: null,
          zipCode: null,
          mobile: null,
          email: null,
          fax: null,
          website: null,
          pinyin: null,
          pinyinInitial: null,
          isDirectory: null,
          isUnit: null,
          isUnitText: null,
          isTemporary: null,
          isTemporaryText: null,
          status: null,
          statusText: null,
          hytx: null,
          dlqy: null,
          orgLevel: null,
          orgLevelText: null,
          orgFunction: null,
          showOrder: null,
          remark: null,
          contactUser: null,
          unitCode: null,
          unitDomain: null,
          cabinetCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      queryPhone() {
        listDepartmentUser(this.deptUser).then(response => {
          this.phoneUser = response.data;
        });
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        getDepartment(row.id).then(response => {
          this.form = response.data;
          this.deptUser.adminGztUserId=null;
          this.deptUser.phone=null;
          this.deptUser.id=row.id;
          this.open = true;
          this.title = "设置调度员";
        });
      },
      /** 提交按钮 */
      submitForm() {
          updateDepartment(this.deptUser).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
      }
    }
  };
</script>
